package com.gx.smart.building;

import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.sysdict.sysdictapi.SysDictApiGrpc;
import com.huishi.gx.ib.grpc.lib.sysdict.sysdictapi.SysDictQueryByParentReq;
import com.huishi.gx.ib.grpc.lib.sysdict.sysdictapi.SysDictQueryByParentResp;
import com.huishi.gx.ib.grpc.lib.sysdict.sysdictapi.SysDictQueryDto;
import com.huishi.gx.ib.grpc.lib.sysdict.sysdictapi.SysDictQueryResponse;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/5/9
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class SysDictApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private SysDictApiService() {

    }

    public static SysDictApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private SysDictApiGrpc.SysDictApiBlockingStub getStub(ManagedChannel channel) {
        return SysDictApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, SysDictQueryResponse> sysDictQuery(CallBack<SysDictQueryResponse> callBack) {
        return new GrpcAsyncTask<String, Void, SysDictQueryResponse>(callBack) {
            @Override
            protected SysDictQueryResponse doRequestData(ManagedChannel channel) {
                SysDictQueryDto message = SysDictQueryDto.newBuilder()
                        .build();
                Logger.d(message);
                SysDictQueryResponse response = null;
                try {
                    response = getStub(channel).sysDictQuery(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "sysDictQuery");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 通过父级的字典编码或者是父级的字典ID查询子集字典列表（只查一级）
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, SysDictQueryByParentResp> queryDictByParentId(
            String parentTag,
            CallBack<SysDictQueryByParentResp> callBack) {
        return new GrpcAsyncTask<String, Void, SysDictQueryByParentResp>(callBack) {
            @Override
            protected SysDictQueryByParentResp doRequestData(ManagedChannel channel) {
                SysDictQueryByParentReq message = SysDictQueryByParentReq.newBuilder()
                        .setParentTag(parentTag)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                SysDictQueryByParentResp response = null;
                try {
                    response = getStub(channel).queryDictByParentId(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryDictByParentId");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    private static class SingleTonHolder {
        private static final SysDictApiService INSTANCE = new SysDictApiService();
    }
}
